package com.wl156.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.wl156.BaseActivity;
import com.wl156.R;
import com.wl156.adapters.MessageAdapter;
import com.wl156.model.Wl156DB;

public class MessageListCenter extends BaseActivity {

	private static final String TAG = MessageListCenter.class.getSimpleName();
	ListView mList;
	String[] list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_internal_message);

		Wl156DB db = new Wl156DB(this);
		Cursor c = db.getReadableDatabase().rawQuery(
				"select * from internal_message order by id desc", null);
		list = new String[c.getCount()];
		int i = 0;
		while (c.moveToNext()) {
			list[i++] = c.getString(0) + "##" + c.getString(1) + "##"
					+ c.getString(2) + "##" + c.getString(3) + "##"
					+ c.getString(4);
		}
		db.close();

		mList = (ListView) findViewById(R.id.list);
		final MessageAdapter adapter = new MessageAdapter(this, list);
		mList.setAdapter(adapter);
		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				Intent i = new Intent(MessageListCenter.this, OneMessage.class);
				i.putExtra("msg", adapter.getItem(position));
				startActivity(i);
			}
		});
		mList.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						MessageListCenter.this);
				builder.setTitle("操作");
				final CharSequence[] items = { "删除" };
				builder.setItems(items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						switch (item) {
						case 0:
							try {
								Wl156DB db = new Wl156DB(MessageListCenter.this);
								db.getWritableDatabase()
										.execSQL(
												"delete from internal_message where id = ?",
												new String[] { String.valueOf(adapter
														.getItemId(position)) });

								Cursor c = db.getReadableDatabase().rawQuery(
										"select * from internal_message", null);
								list = new String[c.getCount()];
								int i = 0;
								while (c.moveToNext()) {
									list[i++] = c.getString(0) + "##"
											+ c.getString(1) + "##"
											+ c.getString(2) + "##"
											+ c.getString(3) + "##"
											+ c.getInt(5);
								}
								db.close();
								
								final MessageAdapter adapter = new MessageAdapter(
										MessageListCenter.this, list);
								mList.setAdapter(adapter);
							} catch (Exception e) {
							}

							break;
						}
					}
				});
				builder.show();
				return true;
			}
		});
	}

	@Override
	public void onBackPressed() {
		MainTab.tabHost.setCurrentTabByTag("home");
	}
}
